<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="房屋编号" prop="houseId">
            <el-input v-model="form.houseId" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="房屋名称" prop="houseName">
            <el-input v-model="form.houseName" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">租售信息</h4>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          v-hasPermi="['domain:type:edit']"
        >修改
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="typeList">
      <el-table-column label="付押类型" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_pay_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="整租/合租" align="center" prop="entireOrShared">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.entire_or_shared" :value="scope.row.entireOrShared"/>
        </template>
      </el-table-column>
      <el-table-column label="起租时长要求/月" align="center" prop="rentalRequest"/>
      <el-table-column label="备注" align="center" prop="remark"/>
    </el-table>

    <h4 class="form-header h4">配套家具</h4>
    <el-table v-loading="loading" :data="supports.slice((pageNum-1)*pageSize,pageNum*pageSize)" ref="table">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="家具名称" align="center" prop="supportName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_house_support" :value="scope.row.supportName"/>
        </template>
      </el-table-column>
    </el-table>

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>

    <!-- 添加或修改租售类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="rentalType" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="付押类型">
              <el-select v-model="rentalType.payType" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.sys_pay_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="整租/合租">
            <el-select v-model="rentalType.entireOrShared" placeholder="请选择">
              <el-option
                v-for="dict in dict.type.entire_or_shared"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="招租人数" prop="needed" v-if="rentalType.entireOrShared === '0'">
            <el-input-number v-model="rentalType.needed" placeholder="请输入招租人数"/>
          </el-form-item>
          <el-form-item label="已租人数" prop="rented" v-if="rentalType.entireOrShared === '0'">
            <el-input-number v-model="rentalType.rented" placeholder="请输入已租人数"/>
          </el-form-item>
        </el-row>
        <el-form-item label="起租时长要求(月)" prop="rentalRequest">
          <el-input-number v-model="rentalType.rentalRequest" placeholder="请输入起租月份"></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="rentalType.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getType, updateType, getTypeInfo} from "@/api/system/rentaltype";

export default {
  name: "Type",
  dicts: ["sys_pay_type", "entire_or_shared", "sys_house_support"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 总条数
      total: 0,
      // 租售类型表格数据
      typeList: [],
      // 配套家具表格数据
      supports: [],
      // 分页信息
      pageNum: 1,
      pageSize: 10,
      //租售信息
      rentalType: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        needed: [
          {required: true, message: "请输入招租人数", trigger: "blur"},
        ],
        rented: [
          {required: true, message: "请输入已租人数", trigger: "blur"},
        ],
      },
      //房屋编号
      houseId: undefined,
      fromPath: '',
    };
  },
  //从指定页面进入当前页，保留之前的输入内容
  beforeRouteEnter(to, from, next) {
    next(vm => {
      // 通过 `vm` 访问组件实例,将值传入fromPath
      vm.fromPath = from.path
    })
  },
  created() {
    //this.getList();
    const houseId = this.$route.params && this.$route.params.houseId;
    if (houseId) {
      this.loading = true;
      getTypeInfo(houseId).then((response) => {
        this.supports = response.supports;
        this.form = response.house;
        this.typeList = response.rentalTypes;
      });
      this.loading = false;
    }
  },
  methods: {
    /** 查询租售类型列表 */
    getList() {
      this.loading = true;
      this.houseId = this.form.houseId;
      getTypeInfo(this.houseId).then((response) => {
        this.typeList = response.rentalTypes;
      });
      this.loading = false;
    },
    /** 修改按钮操作 */
    handleUpdate() {
      getType(this.form.houseId).then(response => {
        this.rentalType = response.rentalType;
        this.open = true;
        this.title = "修改租售类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.rentalType.rentalId != null) {
            updateType(this.rentalType).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 关闭按钮 */
    close() {
      let obj = null;
      if (this.fromPath.indexOf("/related") > 0) {
        obj = {path: "/house/related"};
      } else if (this.fromPath.indexOf("/handleHouse") > 0) {
        obj = {path: "/house/handleHouse"};
      } else {
        obj = {path: "/house/houses"};
      }
      this.$tab.closeOpenPage(obj);
    },
    // 取消按钮
    cancel() {
      this.open = false;
    }
  }
};
</script>
